package org.test.client.ui.component.btn;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.test.client.listener.ClickAction;
import org.test.client.listener.CommonMouseListener;
import org.test.client.ui.util.CompFactory;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

public class MyUIButton extends JButton {
	private MyUIButton thisBtn = this;
	
	public static final int TYPE_BLUE = 0;
	public static final int TYPE_RED = 1;
	public static final int TYPE_GREEN = 2;
	public static final int TYPE_ORANGE = 3;
	public static final int TYPE_DEEP_BLUE = 4;
	
	private static Color blue1 = Icons.getColor("4cb1ea");
	private static Color blue2 = Icons.getColor("239fe6");
	private static Color blueBorder = Icons.getColor("1a89c7");
	
	private static Color green1 = Icons.getColor("84bc46");
	private static Color green2 = Icons.getColor("6b9c35");
	private static Color greenBorder = Icons.getColor("547a29");
	
	private static Color red1 = Icons.getColor("de292f");
	private static Color red2 = Icons.getColor("b81a20");
	private static Color redBorder = Icons.getColor("c71c22");
	
	private static Color orange1 = Icons.getColor("fb6506");
	private static Color orange2 = Icons.getColor("cc4f00");
	private static Color orangeBorder = Icons.getColor("dd5600");
	
	private static Color deepBlue1 = Icons.getColor("044e96");
	private static Color deepBlue2 = Icons.getColor("023361");
	private static Color deepBlueBorder = Icons.getColor("033c73");
	
	
	public static Color[] backs1 = new Color[]{blue1,green1,red1,orange1,deepBlue1};
	public static Color[] backs2 = new Color[]{blue2,green2,red2,orange2,deepBlue2};
	public static Color[] borders = new Color[]{blueBorder,greenBorder,redBorder,orangeBorder,deepBlueBorder};
	
	
	private Color fontAndIconColor = Color.white;
	private Color focusBorderColor = Icons.getColor("ecc579");
	private int btnType = 0;
	
	private String startIconStr = null;
	private String labelStr = null;
	private String endIconStr = null;
	private int iconw = 18;
	
	private BufferedImage startBi = null;
	private BufferedImage endBi = null;
	
	private int diameter = 20;
	//按钮内容 距离 边框的 距离 默认
	private int marginLRDeFault = 13;
	private int marginTBDeFault = 13;
	//若 layout 为null 则  会根据 按钮的 大小 重新计算 距离
	private int marginLR = marginLRDeFault;
	private int marginTB = marginTBDeFault;
	private int gap = 0;
	private int preferw = 0; 
	private int preferh = 0; 
	
	private CommonMouseListener mouseListener = null;
	private boolean pressFlag = false;
	public boolean needFoucsBorder = false;
	
	public MyUIButton(){
		
	}
	
	public MyUIButton(String labelStr){
		this.labelStr = labelStr;
	}
	
	public MyUIButton(String startIconStr, String labelStr, int btnType) {
		this.startIconStr = startIconStr;
		this.labelStr = labelStr;
		this.btnType = btnType;
	}

	public MyUIButton(String startIconStr, String labelStr, String endIconStr) {
		this.startIconStr = startIconStr;
		this.labelStr = labelStr;
		this.endIconStr = endIconStr;
	}



	public MyUIButton doInit(){
		this.setBorder(null);
		this.setOpaque(false);
		
		int start = 0,end = 0;
		if(startIconStr!=null && startIconStr.length()>0){
			startBi = Icons.getImgByDesc(startIconStr, iconw, fontAndIconColor);
			start = iconw + gap;
		}
		if(endIconStr!=null && endIconStr.length()>0){
			endBi = Icons.getImgByDesc(endIconStr, iconw, fontAndIconColor);
			end = iconw + gap;
		}
		
		int strw = 0;
		int strh = 0;
		if(labelStr != null){
			Font font = this.getFont();
			FontMetrics fontMetrics = this.getFontMetrics(font);
			strw = UIUtil.getStringWidth(labelStr, fontMetrics);
			strh = UIUtil.getTxtHeight(fontMetrics);
		}
		preferw = strw + start + end + marginLR*2;
		preferh = (strh>iconw?strh : iconw) + marginTB*2;
		
		this.setPreferredSize(new Dimension(preferw,preferh));
		mouseListener = new CommonMouseListener(this){
			@Override
			public void mousePressed(MouseEvent mouseevent) {
				pressFlag = true;
				thisBtn.repaint();
			}
			
			@Override
			public void mouseReleased(MouseEvent mouseevent) {
				pressFlag = false;
				thisBtn.repaint();
			}
		};
		this.addMouseListener(mouseListener);
		
		return this;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		UIUtil.setAntiAliasing(g2d, true);
		g2d.setStroke(UIUtil.getStroke(1));
		int www = this.getWidth();
		int hhh = this.getHeight();
		
//		System.out.println(www+"|||"+hhh);
		
		if(www > preferw){
			marginLR = (www - preferw)/2 + marginLRDeFault;
		}else{
//			marginLR = ( preferw - www )/ 2;
		}
		
		if(hhh > preferh){
			marginTB =  (hhh - preferh)/2 + marginTBDeFault;
		}
//		else{
//			int tmp = (hhh- (preferh-2*marginTB)) ;
//			if(tmp > 0){
//				marginTB = tmp / 2;
//			}
//		}
		
		int x = 1;
		int y = 1;
		int w = www-2;
		int h = hhh-2;
		Paint paint = UIUtil.getPaint(backs1[btnType], backs2[btnType], x, y, x, y+h);
		g2d.setPaint(paint);
		g2d.fillRoundRect(x, y, w, h, diameter, diameter);

		if(this.isFocusOwner() && needFoucsBorder){
			g2d.setColor(focusBorderColor);
			g2d.drawRoundRect(x, y, w-1, h-1, diameter, diameter);
		}else{
			g2d.setColor(borders[btnType]);
			g2d.drawRoundRect(x, y, w-1, h-1, diameter, diameter);
		}
		
		//draw content
		int cx = marginLR;
		int cy = 0;
		int cw = 0;
		int ch = 0;
		if(startBi != null){
			cy = hhh/2 - startBi.getHeight()/2;
			g2d.drawImage(startBi, cx, cy, null);
			cx = cx + startBi.getWidth()+gap;
		}
		if(labelStr != null){
			//若没有 icon 则 将文字直接写在中间， 不用与 图片 基线对其
			if(startBi == null && endBi == null){
				cy = hhh/2 + UIUtil.getTxty(g2d);
			}else{
				cy = marginTB + g2d.getFontMetrics().getAscent();
			}
			g2d.setColor(fontAndIconColor);
			g2d.drawString(labelStr, cx, cy);
		}
		
		if(endBi != null){
			cy = hhh/2 - endBi.getHeight()/2;
			cx = cx + UIUtil.getTxtWidth(g2d, labelStr)+gap;
			g2d.drawImage(endBi, cx, cy, null);
		}
	
		//draw shadow
		if(pressFlag){
			AlphaComposite ac = UIUtil.getSrcOverAC(0.4f);
			Paint paint2 = UIUtil.getPaint(Color.black, backs2[btnType], x, y, x, y+h);
			g2d.setPaint(paint2);
			g2d.setComposite(ac);
			g2d.fillRoundRect(x+1, y+1, w-2, h-2, diameter, diameter);
		}
		
	}
	
	@Override
	protected void paintBorder(Graphics g) {
//		Graphics2D g2d = (Graphics2D) g;
//		UIUtil.setAntiAliasing(g2d, true);
//		g2d.setStroke(UIUtil.getStroke(1));
//		int x = 1;
//		int y = 1;
//		int www = this.getWidth()-2;
//		int hhh = this.getHeight()-2;
//		
//		if(this.isFocusOwner()){
//			g2d.setColor(focusBorderColor);
//			g2d.drawRoundRect(x, y, www, hhh, diameter, diameter);
//		}else{
//			g2d.setColor(borders[btnType]);
//			g2d.drawRoundRect(x, y, www, hhh, diameter, diameter);
//		}
	}
	
	public int getBtnType() {
		return btnType;
	}

	public void setBtnType(int btnType) {
		this.btnType = btnType;
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		
		
		JPanel jPanel = new JPanel();
//		jPanel.setBackground(Color.black);
		Container contentPane = frame.getContentPane();
		contentPane.add(jPanel);
		
		//blue
		jPanel.add(new MyUIButton("这是一个按钮").doInit());
		jPanel.add(new MyUIButton("fa-plus","按钮","fa-plus").doInit());
		jPanel.add(new MyUIButton("fa-plus","按钮",null).doInit());
		jPanel.add(new MyUIButton(null,"按钮","fa-plus").doInit());
		jPanel.add(new MyUIButton(null,null,"fa-plus").doInit());
		
		//red
		jPanel.add(CompFactory.createRedBtn(null, "这是一个按钮", null));
		jPanel.add(CompFactory.createRedBtn("fa-plus","按钮","fa-plus"));
		jPanel.add(CompFactory.createRedBtn("fa-plus","按钮",null));
		jPanel.add(CompFactory.createRedBtn(null,"按钮","fa-plus"));
		jPanel.add(CompFactory.createRedBtn(null,null,"fa-plus"));
		//green
		jPanel.add(CompFactory.createGreenBtn(null, "这是一个按钮", null));
		jPanel.add(CompFactory.createGreenBtn("fa-plus","按钮","fa-plus"));
		jPanel.add(CompFactory.createGreenBtn("fa-plus","按钮",null));
		jPanel.add(CompFactory.createGreenBtn(null,"按钮","fa-plus"));
		jPanel.add(CompFactory.createGreenBtn(null,null,"fa-plus"));
		//orange
		jPanel.add(CompFactory.createOrangeBtn(null, "这是一个按钮", null));
		jPanel.add(CompFactory.createOrangeBtn("fa-plus","按钮","fa-plus"));
		jPanel.add(CompFactory.createOrangeBtn("fa-plus","按钮",null));
		jPanel.add(CompFactory.createOrangeBtn(null,"按钮","fa-plus"));
		jPanel.add(CompFactory.createOrangeBtn(null,null,"fa-plus"));
		//deepBlue
		jPanel.add(CompFactory.createDeepBlueBtn(null, "这是一个按钮", null));
		jPanel.add(CompFactory.createDeepBlueBtn("fa-plus","按钮","fa-plus"));
		jPanel.add(CompFactory.createDeepBlueBtn("fa-plus","按钮",null));
		jPanel.add(CompFactory.createDeepBlueBtn(null,"按钮","fa-plus"));
		jPanel.add(CompFactory.createDeepBlueBtn(null,null,"fa-plus"));
		
		
		////////////////top 
		JPanel top = new JPanel();
		top.setPreferredSize(new Dimension(0,90));
		contentPane.add(top,BorderLayout.NORTH);
		
		MyUIButton top1 = CompFactory.createDeepBlueBtn(null, "这是一个按钮", null);
		MyUIButton top2 = CompFactory.createDeepBlueBtn("fa-plus","按钮","fa-plus");
		top.setLayout(null);
		top.add(top1);
		top.add(top2);
		
		top1.setBounds(10, 10, 100, 80);
		top2.setBounds(150, 10, 250, 50);
		
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		frame.setSize(512, 400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

}
